package com.bfl.sa.common.web.validate;


import com.bfl.sa.common.web.entity.ErrorCode;

public class AjaxResponse {

    private ErrorCode code = ErrorCode.OK;
    private Boolean success = Boolean.TRUE;
    private String message;
    private Object data;
    private Object error;

    private AjaxResponse() {}

    private AjaxResponse(Object data) {
        this.message = "操作成功";
        this.data = data;
    }

    private AjaxResponse(ErrorCode code, String message) {
        this.code = code;
        this.message = message;
        if (this.message == null) {
            if (code != ErrorCode.OK) {
                this.success = Boolean.FALSE;
                this.message = "操作失败";
            }
            if (code == ErrorCode.OK) {
                this.success = Boolean.TRUE;
                this.message = "操作成功";
            }
        }
    }

    private AjaxResponse(ErrorCode code, Object error) {
        this.code = code;
        this.error = error;
        if (code != ErrorCode.OK) {
            this.success = Boolean.FALSE;
            this.message = "操作失败";
        }
        if (code == ErrorCode.OK) {
            this.success = Boolean.TRUE;
            this.message = "操作成功";
        }
    }

    public static AjaxResponse ok() {
        return ok(null);
    }

    public static AjaxResponse ok(String message) {
        return new AjaxResponse(ErrorCode.OK, message);
    }

    public static AjaxResponse ok(Object data) {
        return new AjaxResponse(data);
    }

    public static AjaxResponse fail() {
        return fail(null);
    }

    public static AjaxResponse fail(ErrorCode code, String message) {
        return new AjaxResponse(code, message);
    }

    public static AjaxResponse fail(Object error) {
        return new AjaxResponse(ErrorCode.FAIL, error);
    }

    public static AjaxResponse error() {
        return error(null);
    }

    public static AjaxResponse error(String message) {
        return new AjaxResponse(ErrorCode.ERROR, message);
    }

    public static AjaxResponse error(ErrorCode code, String message) {
        return new AjaxResponse(code, message);
    }

    public static AjaxResponse error(Object error) {
        return new AjaxResponse(ErrorCode.ERROR, error);
    }

    public Integer getCode() {
        return code.getCode();
    }

    public void setCode(ErrorCode code) {
        this.code = code;
    }

    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    public Object getError() {
        return error;
    }

    public void setError(Object errors) {
        this.error = errors;
    }
}
